package ua.khpi.si.kit49.diploma.db.dto;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * Class represents user roles for users of this system
 * 
 * @author netstalk3r
 * 
 */

@Entity
@Table(name = "user_roles")
public class UserRole {

	@Id
	@GeneratedValue
	@Column(name = "user_role_id", unique = true, nullable = false)
	private Integer userRoleId;

	@OneToOne(cascade = CascadeType.ALL)
	@Fetch(FetchMode.JOIN)
	@JoinColumn(name = "user_id", unique = true, nullable = false)
	private User user;

	@Column(name = "authority", unique = true, nullable = false, length = 45)
	private String authority;

	public UserRole() {
		super();
	}

	public UserRole(Integer userId, String authority, User user) {
		super();
		this.authority = authority;
		this.user = user;
	}

	public Integer getUserRoleId() {
		return userRoleId;
	}

	public void setUserRoleId(Integer userRoleId) {
		this.userRoleId = userRoleId;
	}

	public String getAuthority() {
		return authority;
	}

	public void setAuthority(String authority) {
		this.authority = authority;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String toString() {
		return "UserRole: userRoleId - " + userRoleId + "; userId - " +
		// userId
				"; authority - " + authority;
	}

	@Override
	public int hashCode() {
		int hash = 33;
		hash = hash * 19 + ((userRoleId == null) ? 0 : userRoleId.hashCode());
		hash = hash * 19 + ((user == null) ? 0 : user.hashCode());
		hash = hash * 19 + ((authority == null) ? 0 : authority.hashCode());
		return hash;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof UserRole))
			return false;
		UserRole entry = (UserRole) obj;
		return userRoleId.equals(entry.getUserRoleId())
				&& user.equals(entry.getUser())
				&& authority.equals(entry.getAuthority());
	}
}
